﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
namespace WPFExcelReport
{
    public partial class TWZ04 : UIWBBST
    {
        #region ==VVT===========
        public static readonly DependencyProperty VVTProperty = DependencyProperty.Register("VVT",
            typeof(string), typeof(TWZ04),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnVVTChanged));
        public string VVT
        {
            get
            {
                return (string)GetValue(VVTProperty);
            }
            set
            {
                SetValue(VVTProperty, value);
            }
        }
        private static void OnVVTChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TWZ04;
            if (src != null)
            {
                src.VVT = (string)e.NewValue;
            }
        }
        #endregion

        #region ==SLO===========
        public static readonly DependencyProperty SLOProperty = DependencyProperty.Register("SLO",
            typeof(string), typeof(TWZ04),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnSLOChanged));
        public string SLO
        {
            get
            {
                return (string)GetValue(SLOProperty);
            }
            set
            {
                SetValue(SLOProperty, value);
            }
        }
        private static void OnSLOChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TWZ04;
            if (src != null)
            {
                src.SLO = (string)e.NewValue;
            }
        }
        #endregion

        #region ==SLB===========
        public static readonly DependencyProperty SLBProperty = DependencyProperty.Register("SLB",
            typeof(string), typeof(TWZ04),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnSLBChanged));
        public string SLB
        {
            get
            {
                return (string)GetValue(SLBProperty);
            }
            set
            {
                SetValue(SLBProperty, value);
            }
        }
        private static void OnSLBChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TWZ04;
            if (src != null)
            {
                src.SLB = (string)e.NewValue;
            }
        }
        #endregion

        #region ==LIEFSCH===========
        public static readonly DependencyProperty LIEFSCHProperty = DependencyProperty.Register("LIEFSCH",
            typeof(string), typeof(TWZ04),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnLIEFSCHChanged));
        public string LIEFSCH
        {
            get
            {
                return (string)GetValue(LIEFSCHProperty);
            }
            set
            {
                SetValue(LIEFSCHProperty, value);
            }
        }
        private static void OnLIEFSCHChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TWZ04;
            if (src != null)
            {
                src.LIEFSCH = (string)e.NewValue;
            }
        }
        #endregion

        #region ==STKNR===========
        public static readonly DependencyProperty STKNRProperty = DependencyProperty.Register("STKNR",
            typeof(string), typeof(TWZ04),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnSTKNRChanged));
        public string STKNR
        {
            get
            {
                return (string)GetValue(STKNRProperty);
            }
            set
            {
                SetValue(STKNRProperty, value);
            }
        }
        private static void OnSTKNRChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TWZ04;
            if (src != null)
            {
                src.STKNR = (string)e.NewValue;
            }
        }
        #endregion

        public TWZ04()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VWBBST();
            MESSAGES += "WBVVT;";
            this.DataContext = MV;
            InitializeComponent();
        } 
        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            if (dwMaster == null)
                return;
            //if (app.User.Trim() == "JLTV" || app.User.Trim() == "ITXV")
            //{
               STKNR = dwMaster["WBBSTKNR"].ToString();
            //}
        }

        public override void OnWorkflowIDChange(MessageEventArgs e)
        {
            base.OnWorkflowIDChange(e);
            MessageInfo msg = e.Info;
            string task = msg.MSGTYPE;
            switch (task)
            {
                case "WBVVT":
                    LIEFSCH = msg.LIEFSCH;
                    VVT = msg.KRS + msg.LFD + msg.POS + SLO + SLB + msg.LIEFSCH;
                    if (LastLIEFSCH != VVT)
                    {
                        msg.LO = SLO;
                        msg.LB = SLB;
                        VVTLOAD(msg);
                        LastLIEFSCH = VVT;
                    }
                    Append(msg);
                    break;
                case "VVTLOAD":
                    msg.LO = SLO;
                    msg.LB = SLB;
                    LIEFSCH = msg.LIEFSCH;
                    VVT = msg.KRS + msg.LFD + msg.POS + SLO + SLB + msg.LIEFSCH;
                    if (LastLIEFSCH != VVT)
                    {
                        msg.LO = SLO;
                        msg.LB = SLB;
                        VVTLOAD(msg);
                        LastLIEFSCH = VVT;
                    }
                    break;
            }
        }

        string LastLIEFSCH = "";
        public void Append(MessageInfo msg)
        {
            if (msg.P2 == "")
                msg.P2 = "0";
            filter = "WBBSTKNR='" + msg.STKNR + "' AND WBBLO='" + LO + "' AND WBBLB='" + LB + "'";
            rows = TBMain.Select(filter);
            if (rows.Length > 0)
            {
                MessageBox.Show("已经发货");
                return;
            }
            filter = "WBBSTKNR='" + msg.STKNR + "'";
            rows = TBMain.Select(filter); 
            if (rows.Length > 0)
                return;
            dwData = TBMain.NewRow();
            dwData["WBBMANDANT"] = app.Mandant;
            dwData["WBBSTKNR"] = msg.STKNR;
            SResult = TMV.QueryWBBST(dwData, 13);
            foreach (DataRow row in SResult.Tables[0].Rows)
            {
                dwData.ItemArray = row.ItemArray;
                TBMain.Rows.InsertAt(dwData, 0);
                break;
            }
        } 
        public void VVTLOAD(MessageInfo msg)
        {
            if (LIEFSCH.Length < 1)
            {
                TBMain.Clear();
                return;
            }
            if (msg == null)
                return;
            DataSet SResult;
            SResult = TMV.WB2WH(msg);
            MergeData(SResult, true);
        }

        public void WV03ALL()
        {
            try
            {
                MV.FTRESET = false;
                foreach (DataRow _dwWB in TBMain.Rows)
                {
                    if (_dwWB["WBBLB"].ToString() == "KO")
                        continue;
                    MOVE(_dwWB);
                }
                
                dwData = VVVLIEF.Instance.TBMain.NewRow();
                dwData["VVLIEFSCH"] = TBMain.Rows[0]["WBBALIEFSCH"].ToString();
                dwData["VVVERSDAT"]=INTEXDBC.GetDBDATE();
                VVVLIEF.Instance.UpdateVVLIEF(dwData,6);
                MV.FTRESET = true;
                TBMain.AcceptChanges();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        public void Free()
        {
            vwMaster.View.MoveCurrentTo(-1);
            filter = "WBBSTKNR='" + STKNR + "'";
            rows = TBMain.Select(filter);
            foreach (DataRow row in rows)
            {
                row.Delete();
            }
            TBMain.AcceptChanges();
        }

        public void WV03ALLCommand(object sender, RoutedEventArgs e)
        {
            WV03ALL();
            base.NewCommand(sender, e);
        }

        public void MOVE(DataRow _dwWB)
        {
            TMV.MOVE2KO(_dwWB, LO, LB, LP, this.RUNID);
        }

        private void STKNR_KeyUp(object sender, KeyEventArgs e)
        {
            this.Focus();
            if (e.Key != System.Windows.Input.Key.Enter)
                return;
            if (e.Key != System.Windows.Input.Key.Return)
                return;
            filter = "WBBSTKNR='" + STKNR + "' AND WBBLO='" + SLO + "' AND WBBLB='" + SLB + "'";
            rows = TBMain.Select(filter);
            if (rows.Length == 0)
            {
                System.Media.SystemSounds.Beep.Play();
                return;
            }
            MOVE(rows[0]);
            System.Media.SystemSounds.Asterisk.Play();
            STKNR = "";
            TextBox txt = sender as TextBox;
            txt.Focus();
        }

        public override void DeleteCommand(object sender, RoutedEventArgs e)
        {
            Free();
        }

        private void LIEFSCH_KeyUp(object sender, KeyEventArgs e)
        {
            this.Focus();
            if (e.Key != System.Windows.Input.Key.Enter)
                return;
            if (e.Key != System.Windows.Input.Key.Return)
                return;
            VVTLOAD(null);
        } 

        public override void LOADBYAUF()
        {
            TBMain.Clear();
            if (ORDERNUM.Length < 20)
                return;
            LIEFSCH = ORDERNUM.Substring(12, 8);
            PM["AUFTYP"] = ORDERNUM.Substring(0, 1);
            PM["KRS"] = ORDERNUM.Substring(1, 2);
            PM["LFD"] = ORDERNUM.Substring(3, 6);
            PM["POS"] = ORDERNUM.Substring(9, 3);
            PM["LIEFSCH"] = ORDERNUM.Substring(12, 8);
            PM["LO"] = SLO;
            PM["LB"] = SLB;
            SResult = TMV.WB2WH(PM);
            MergeData(SResult, true, true);
            base.LOADBYAUF();
        }
    }
}
